<template>
  <div>
    <el-form class="query-form" @submit.native.prevent :inline="true">
      <el-form-item>
        <select-address
          v-model="params['customer_address_code.codelike']"
          auto-top
          clearable
          placeholder="所属地"
          @change="getData()"
        ></select-address>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.business_name"
          :clearable="true"
          placeholder="经营者查询"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="params.submit_person_name"
          :clearable="true"
          placeholder="操作用户"
          @change="getData()"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>

      <el-form-item class="float-right">
        <el-button type="primary" icon="el-icon-upload2" @click="handleUpload()">导入</el-button>
        <el-button
          type="primary"
          icon="el-icon-download"
          :loading="exportLoading"
          @click="handleDownload()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <import-excel
      :visible.sync="importDialogVisible"
      :title="`导入${dataName}`"
      :post-api="importUrl"
      :excel-template="`/static/files/${dataName}导入模板.xlsx`"
      @success="getData(params.index)"
    ></import-excel>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SelectAddress from '@/components/select/SelectAddress'
import ImportExcel from '@/components/ImportExcel'

export default {
  components: {
    SelectAddress,
    ImportExcel,
  },
  props: {},
  data() {
    return {
      loading: false,
      tableName: 'datum_customer_finance_capital_source_import',
      dataName: '补贴',
      params: {
        index: 1,
        size: 20,
        customer_name: '',
        submit_person_name: '',
        'customer_address_code.codelike': '',
      },

      tableData: [],
      tableCount: 0,
      tableColumns: [
        { attrs: { label: '经营者姓名', prop: 'business_name' } },
        { attrs: { label: '身份证号码', prop: 'lender_idcard' } },
        {
          attrs: { label: '补贴时间', prop: 'capital_date', align: 'center' },
          process: v => this.$utils.dateFormat(v, 'yyyy.MM.dd') || '-',
        },
        { attrs: { label: '补贴机构', prop: 'capital_org', align: 'center' } },
        { attrs: { label: '补偿名称', prop: 'capital_source' } },
        { attrs: { label: '资金用途', prop: 'capital_use' } },
        { attrs: { label: '金额(万元）', prop: 'funds' } },
        { attrs: { label: '备注', prop: 'remark' } },
      ],

      // 导入、导出
      importDialogVisible: false,
      importUrl: '',
      submit_type: '',
      exportLoading: false,
    }
  },
  computed: {
    ...mapGetters(['org']),
  },
  watch: {},
  created() {
    this.params['customer_address_code.codelike'] = this.$utils.getAddressCode()
    this.importUrl = `/api/credit/import/?importKey=${this.tableName}_template`
    this.submit_type = this.$utils.getClientOrgClass() === 'org_gov_ny' ? '1' : '4'
    this.params['submit_type.eq'] = this.submit_type
    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, `${this.tableName}_view`, this.params)
      )
      this.loading = false
      const data = (res && res.data) || {}
      this.tableData = data.data || []
      this.tableCount = data.totalCount || 0
    },
    handleUpload() {
      this.importDialogVisible = true
    },
    async handleDownload() {
      const { index, size, ...params } = this.params // eslint-disable-line
      const exportParams = {
        service: this.$serviceName,
        table: `${this.tableName}_view`,
        params: params,
        filename: `${this.dataName}.xlsx`,
      }
      this.exportLoading = true
      await this.$utils.promise(this.$api.exportData(exportParams))
      this.exportLoading = false
    },
  },
}
</script>

<style lang="scss" scoped></style>
